<?php
/**
 * Library Of Shared Code (LOSC)
 *
 * LICENSE
 *
 * This source file is subject to the New BSD License that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */

/**
 * Helper for a simple toolbar
 *
 * @category   LOSC Framework
 * @package    View
 * @subpackage Helper
 * @copyright  Copyright (c) 2008 Robin Skoglund (http://robinsk.net/)
 * @license    http://creativecommons.org/licenses/BSD/  New BSD License
 */
class Losc_View_Helper_Sidebar extends Zend_View_Helper_Abstract
{
    const COLLAPSED  = 'collapsed';
    const EXPANDED   = 'expanded';
    const NOCOLLAPSE = 'no';
    
    /**
     * What string to use as the indentation of output, this will typically be
     * spaces. e.g. '    '
     * 
     * @var string
     */
    private $_indent = '';
    
    /**
     * Sidebar widgets
     *
     * @var array
     */
    private $_widgets = array();
    
    /**
     * Collapse status for widgets
     *
     * @var array
     */
    private $_collapse = array();

    /**
     * Retrieve placeholder for title element and optionally set state
     *
     * @param  string       $title  [optional] widget title
     * @param  string|array $item   [optional] widget content
     * @param  string       $coll   [optional] widget collapse status, one of
     *                              'collapse', 'expand' or false 
     * @return Losc_View_Helper_Sidebar
     */
    public function sidebar($title = null, $item = null, $coll = null)
    {
        if ($title) {
            if ($item) {
                if (!isset($this->_widgets[$title])) {
                    $this->_widgets[$title] = array();
                }
                
                if (is_array($item)) {
                    foreach ($item as $widgetItem) {
                        $this->_widgets[$title][] = $widgetItem;
                    }
                } else {
                    $this->_widgets[$title][] = $item;
                }
            }
            
            //if (!array_key_exists($title, $this->_collapse)) {
            if (!isset($this->_collapse[$title])) {
                $this->_collapse[$title] = self::EXPANDED;
            }
            
            switch ($coll) {
                case self::EXPANDED:
                case self::COLLAPSED:
                case self::NOCOLLAPSE:
                    $this->_collapse[$title] = $coll;
                    break;
            }
        }

        return $this;
    }

    /**
     * Set the indentation string for __toString() serialization,
     * optionally, if a number is passed, it will be the number of spaces
     *
     * @param  string|int $indent
     * @return Zend_View_Helper_Placeholder_Container_Abstract
     */
    public function setIndent($indent)
    {
        $this->_indent = $this->getWhitespace($indent);
        return $this;
    }

    /**
     * Retrieve indentation
     *
     * @return string
     */
    public function getIndent()
    {
        return $this->_indent;
    }

    /**
     * Retrieve whitespace representation of $indent
     * 
     * @param  int|string $indent 
     * @return string
     */
    public function getWhitespace($indent)
    {
        if (is_int($indent)) {
            $indent = str_repeat(' ', $indent);
        }

        return (string) $indent;
    }

    /**
     * Turn helper into string
     *
     * @param  string|null $indent
     * @param  string|null $locale
     * @return string
     */
    public function toString($indent = null)
    {
        $indent = (null !== $indent)
                ? $this->getWhitespace($indent)
                : $this->getIndent();
        
        if (count($this->_widgets) < 1) {
            return '';
        }
        
        $output = '';
        
        foreach ($this->_widgets as $title => $widget) {
            if (count($widget)) {
                $content = "$indent        <ul>\n";
                foreach ($widget as $item) {
                    $content .= "$indent            <li>$item</li>\n";
                }
                $content .= "$indent        </ul>";
            } else {
                $content = '';
            }
            
            switch ($this->_collapse[$title]) {
                case self::EXPANDED:
                    $titleCss = 'collapse';
                    $contentCss = '';
                    break;
                case self::COLLAPSED:
                    $titleCss = 'expand';
                    $contentCss = 'hidden';
                    break;
                default:
                    $titleCss = '';
                    $contentCss = '';
                    break;
            }
            
            $output .= <<<EOT
$indent<div class="widget">
$indent    <div class="header">
$indent        <div class="rounded">
$indent            <div class="l"></div>
$indent            <div class="r"></div>
$indent            <div class="m"></div>
$indent        </div>
$indent        <div class="wrapper1">
$indent            <div class="wrapper2">
$indent                <div class="wrapper3">
$indent                    <div class="wrapper4 $titleCss">
$indent                        <h3>$title</h3>
$indent                    </div>
$indent                </div>
$indent            </div>
$indent        </div>
$indent    </div>
$indent    <div class="content $contentCss">
$content
$indent    </div>
$indent</div>

EOT;
        }

        return $output;
    }
}
